

#ifndef _CircularMenu_H_
#define _CircularMenu_H_

#include "WidgetsApi.hpp"
#include "../OLCore/AbstractWidget.hpp"
#include "../OLApp/defines.hpp"

#include <QPainterPath>
#include <QMouseEvent>
#include <QMenuBar>
#include <QWidget>
#include <QPainter>
#include <QPen>

namespace NSWidgets {

	class WIDGETSAPI CircularMenu :
		public NSCore::AbstractWidget
{
		Q_OBJECT

	public:
		CircularMenu(QWidget* w = nullptr);
		~CircularMenu();

	public: /*override*/
		virtual inline QString className() override { return "CircularMenu"; };

	public:
        void paintEvent(QPaintEvent* event) override
        {
            Q_UNUSED(event);

            QPainter painter(this);
            painter.setRenderHint(QPainter::Antialiasing);

            int centerX = width() / 2;
            int centerY = height() / 2;
            int outerRadius = 100;
            int innerRadius = 60;
            int numSegments = 8; // Number of equal segments

            qreal segmentAngle = 360.0 / numSegments;

            for (int i = 0; i < numSegments; ++i) {
                QPainterPath segmentPath;

                // Calculate start and span angles for the segment
                qreal startAngle = i * segmentAngle;
                qreal spanAngle = segmentAngle;

                // Create path for the segment
                QRectF outerRect(centerX - outerRadius, centerY - outerRadius,
                    outerRadius * 2, outerRadius * 2);
                QRectF innerRect(centerX - innerRadius, centerY - innerRadius,
                    innerRadius * 2, innerRadius * 2);

                segmentPath.arcMoveTo(outerRect, -startAngle); // Qt uses clockwise angles, starting from 0 degrees (right)
                segmentPath.arcTo(outerRect, -startAngle, -spanAngle); // Add outer arc
                segmentPath.arcTo(innerRect, -(startAngle + spanAngle), spanAngle); // Add inner arc (connect back)
                segmentPath.closeSubpath(); // Close the path

                // Set colors based on hover state
                if (i == m_hoveredSegment) {
                    painter.setBrush(QColor(100, 150, 255)); // Lighter blue on hover
                    painter.setPen(QPen(Qt::darkBlue, 2));
                }
                else {
                    painter.setBrush(QColor(50, 100, 200)); // Normal blue
                    painter.setPen(QPen(Qt::black, 1));
                }

                painter.drawPath(segmentPath);
            }
        }

        void mouseMoveEvent(QMouseEvent* event) override
        {
            int centerX = width() / 2;
            int centerY = height() / 2;
            int outerRadius = 100;
            int innerRadius = 60;
            int numSegments = 8;
            qreal segmentAngle = 360.0 / numSegments;

            // Calculate relative coordinates from center
            qreal x = event->pos().x() - centerX;
            qreal y = centerY - event->pos().y(); // Invert Y axis for standard mathematical coordinates

            // Check if mouse is within the ring
            qreal distanceSquared = x * x + y * y;
            if (distanceSquared > innerRadius * innerRadius && distanceSquared < outerRadius * outerRadius) {
                // Calculate angle in degrees (0 to 360)
                qreal angle = std::atan2(y, x) * 180.0 / 3.1415926;
                if (angle < 0) {
                    angle += 360.0;
                }

                // Determine which segment is hovered
                int newHoveredSegment = static_cast<int>(angle / segmentAngle) % numSegments;

                if (newHoveredSegment != m_hoveredSegment) {
                    m_hoveredSegment = newHoveredSegment;
                    update(); // Trigger repaint
                }
            }
            else {
                // Mouse is outside the ring
                if (m_hoveredSegment != -1) {
                    m_hoveredSegment = -1;
                    update(); // Trigger repaint
                }
            }
            QWidget::mouseMoveEvent(event);
        }

        void leaveEvent(QEvent* event) override
        {
            // Reset hover when mouse leaves the widget
            if (m_hoveredSegment != -1) {
                m_hoveredSegment = -1;
                update();
            }
            QWidget::leaveEvent(event);
        }

private:
    int m_hoveredSegment; // Index of the currently hovered segment, -1 if none

	};

}

#endif

